﻿using System.Windows;
using System.Windows.Controls;

namespace Sidvall.UI.Wpf.Controls
{
    public partial class HeaderedTextBox : UserControl
    {
        #region Public Members

        #region LabelMargin

        public static readonly System.Windows.DependencyProperty LabelMarginProperty = System.Windows.DependencyProperty.Register(nameof(LabelMargin), typeof(Thickness), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(new Thickness(0, 0, 0, 0), System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public Thickness LabelMargin
        {
            get
            {
                return (Thickness)GetValue(LabelMarginProperty);
            }
            set
            {
                SetValue(LabelMarginProperty, value);
            }
        }

        #endregion
        #region LabelText

        public static readonly System.Windows.DependencyProperty LabelTextProperty = System.Windows.DependencyProperty.Register(nameof(LabelText), typeof(string), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None, OnLabelTextPropertyChanged, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public string LabelText
        {
            get
            {
                return GetValue(LabelTextProperty) as string;
            }
            set
            {
                SetValue(LabelTextProperty, value);
            }
        }

        #endregion
        #region LabelLeftContent

        public static readonly System.Windows.DependencyProperty LabelLeftContentProperty = System.Windows.DependencyProperty.Register(nameof(LabelLeftContent), typeof(object), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public object LabelLeftContent
        {
            get
            {
                return GetValue(LabelLeftContentProperty);
            }
            set
            {
                SetValue(LabelLeftContentProperty, value);
            }
        }

        #endregion
        #region LabelRightContent

        public static readonly System.Windows.DependencyProperty LabelRightContentProperty = System.Windows.DependencyProperty.Register(nameof(LabelRightContent), typeof(object), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public object LabelRightContent
        {
            get
            {
                return GetValue(LabelRightContentProperty);
            }
            set
            {
                SetValue(LabelRightContentProperty, value);
            }
        }

        #endregion
        #region Text

        public static readonly System.Windows.DependencyProperty TextProperty = System.Windows.DependencyProperty.Register(nameof(Text), typeof(string), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(null, System.Windows.FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, false, System.Windows.Data.UpdateSourceTrigger.LostFocus));
        public string Text
        {
            get
            {
                return GetValue(TextProperty) as string;
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        #endregion
        #region TextBoxAcceptsReturn

        public static readonly System.Windows.DependencyProperty TextBoxAcceptsReturnProperty = System.Windows.DependencyProperty.Register(nameof(TextBoxAcceptsReturn), typeof(bool), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(false, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public bool TextBoxAcceptsReturn
        {
            get
            {
                return (bool)GetValue(TextBoxAcceptsReturnProperty);
            }
            set
            {
                SetValue(TextBoxAcceptsReturnProperty, value);
            }
        }

        #endregion
        #region TextBoxBackground

        public static readonly System.Windows.DependencyProperty TextBoxBackgroundProperty = System.Windows.DependencyProperty.Register(nameof(TextBoxBackground),
            typeof(System.Windows.Media.Brush),
            typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(System.Windows.Media.Brushes.White, System.Windows.FrameworkPropertyMetadataOptions.None,
                null, null, false, System.Windows.Data.UpdateSourceTrigger.LostFocus));
        public System.Windows.Media.Brush TextBoxBackground
        {
            get
            {
                return GetValue(TextBoxBackgroundProperty) as System.Windows.Media.Brush;
            }
            set
            {
                SetValue(TextBoxBackgroundProperty, value);
            }
        }

        #endregion
        #region TextBoxBorderBrush

        public static readonly System.Windows.DependencyProperty TextBoxBorderBrushProperty = System.Windows.DependencyProperty.Register(nameof(TextBoxBorderBrush), typeof(System.Windows.Media.Brush), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 171, 173, 179)), System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.LostFocus));
        public System.Windows.Media.Brush TextBoxBorderBrush
        {
            get
            {
                return GetValue(TextBoxBorderBrushProperty) as System.Windows.Media.Brush;
            }
            set
            {
                SetValue(TextBoxBorderBrushProperty, value);
            }
        }

        #endregion
        #region TextBoxBorderThickness

        public static readonly System.Windows.DependencyProperty TextBoxBorderThicknessProperty = System.Windows.DependencyProperty.Register(nameof(TextBoxBorderThickness), typeof(Thickness), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(new Thickness(1), System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public Thickness TextBoxBorderThickness
        {
            get
            {
                return (Thickness)GetValue(TextBoxBorderThicknessProperty);
            }
            set
            {
                SetValue(TextBoxBorderThicknessProperty, value);
            }
        }

        #endregion
        #region TextBoxFontWeight

        public static readonly System.Windows.DependencyProperty TextBoxFontWeightProperty = System.Windows.DependencyProperty.Register(nameof(TextBoxFontWeight), typeof(System.Windows.FontWeight), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(System.Windows.FontWeights.Normal, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public System.Windows.FontWeight TextBoxFontWeight
        {
            get
            {
                return (System.Windows.FontWeight)GetValue(TextBoxFontWeightProperty);
            }
            set
            {
                SetValue(TextBoxFontWeightProperty, value);
            }
        }

        #endregion
        #region TextBoxHeight

        public static readonly System.Windows.DependencyProperty TextBoxHeightProperty = System.Windows.DependencyProperty.Register(nameof(TextBoxHeight), typeof(double), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(System.Double.NaN, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public double TextBoxHeight
        {
            get
            {
                return (double)GetValue(TextBoxHeightProperty);
            }
            set
            {
                SetValue(TextBoxHeightProperty, value);
            }
        }

        #endregion
        #region TextBoxIsEnabled

        public static readonly System.Windows.DependencyProperty TextBoxIsEnabledProperty = System.Windows.DependencyProperty.Register(nameof(TextBoxIsEnabled), typeof(bool), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(true, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public bool TextBoxIsEnabled
        {
            get
            {
                return (bool)GetValue(TextBoxIsEnabledProperty);
            }
            set
            {
                SetValue(TextBoxIsEnabledProperty, value);
            }
        }

        #endregion
        #region TextBoxIsReadOnly

        public static readonly System.Windows.DependencyProperty TextBoxIsReadOnlyProperty = System.Windows.DependencyProperty.Register(nameof(TextBoxIsReadOnly), typeof(bool), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(false, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public bool TextBoxIsReadOnly
        {
            get
            {
                return (bool)GetValue(TextBoxIsReadOnlyProperty);
            }
            set
            {
                SetValue(TextBoxIsReadOnlyProperty, value);
            }
        }

        #endregion
        #region TextBoxVerticalScrollBarVisibility

        public static readonly System.Windows.DependencyProperty TextBoxVerticalScrollBarVisibilityProperty = System.Windows.DependencyProperty.Register(nameof(TextBoxVerticalScrollBarVisibility), typeof(ScrollBarVisibility), typeof(HeaderedTextBox),
            new System.Windows.FrameworkPropertyMetadata(ScrollBarVisibility.Hidden, System.Windows.FrameworkPropertyMetadataOptions.None, null, null, false, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
        public ScrollBarVisibility TextBoxVerticalScrollBarVisibility
        {
            get
            {
                return (ScrollBarVisibility)GetValue(TextBoxVerticalScrollBarVisibilityProperty);
            }
            set
            {
                SetValue(TextBoxVerticalScrollBarVisibilityProperty, value);
            }
        }

        #endregion

        #endregion
        #region Private Members

        #region InitializeLabel

        private static void InitializeLabel(HeaderedTextBox parent)
        {
            if (string.IsNullOrWhiteSpace(parent.LabelText))
            {
                parent.Label.Visibility = Visibility.Collapsed;
            }
            else
            {
                parent.Label.Visibility = Visibility.Visible;
            }
        }

        #endregion

        #region OnLabelTextPropertyChanged

        private static void OnLabelTextPropertyChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
        {
            var parent = d as HeaderedTextBox;
            if (parent == null)
                return;
            InitializeLabel(parent);
        }

        #endregion

        #endregion
        #region Constructors

        public HeaderedTextBox()
        {
            InitializeComponent();
        }

        #endregion
    }
}
